package com.minhang.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 员工实体类
 *
 * @author rookie
 */
@Data
public class Employee implements UserDetails {

    /**
     * id
     */
    private Integer id;


    /**
     * 姓名
     */
    private String name;

    /**
     * 性别
     */
    private String sex;


    /**
     * 密码
     */
    @JsonIgnore
    private String pwd;


    /**
     * 个人邮箱
     */
    private String personalEmail;

    /**
     * 企业邮箱
     */
    private String enterpriseEmail;

    /**
     * 岗位id
     */
    private Integer postId;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 修改时间
     */
    private Date updateTime;

    /**
     * 创建人
     */
    private String createUser;

    /**
     * 状态 0正常 1其他待定 2 运维端管理员
     */
    private Integer state;


    /**
     * 所拥有的角色信息
     */
    private List<Role> roles = new ArrayList<>();

    /**
     * 角色id集合
     */
    private List<Integer> roleIds = new ArrayList<>();



    /**
     * 权限
     *
     * @return
     */
    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>(roles.size());
        for (Role role : roles) {
            authorities.add(new SimpleGrantedAuthority(role.getRoleCode()));
        }
        return authorities;
    }

    /**
     * 密码
     *
     * @return
     */
    @JsonIgnore
    @Override
    public String getPassword() {
        return this.pwd;
    }

    /**
     * 用户名
     *
     * @return
     */
    @JsonIgnore
    @Override
    public String getUsername() {
        return this.enterpriseEmail;
    }

    /**
     * 用户账号是否过期
     *
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 用户账号是否被锁定
     *
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 用户密码是否过期
     *
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 用户是否可用
     *
     * @return
     */
    @Override
    public boolean isEnabled() {
        return this.state != null && (this.state == 0 || this.state == 2);
    }


}
